// spells.c++
// spell utility functions implementation file

#include <cmath>

#include "highground.h"
#include "map.h"
#include "spells.h"

extern Map* map;

void adjustTile(const unsigned int x, const unsigned int y,
                const signed int dHeight) {
	if ((signed int)map->getTile(0, x, y) + dHeight < 0) {
		map->setTile(0, x, y, 0);
	} else if ((signed int)map->getTile(0, x, y) + dHeight > 255) {
		map->setTile(0, x, y, 255);
	} else {
		map->setTile(0, x, y, map->getTile(0, x, y) + dHeight);
	}
}

void buildHill(const unsigned int centreX, const unsigned int centreY,
               const unsigned int radius, const signed int height) {
	unsigned int radSq = radius * radius;
	for (unsigned int y = 0; y < map->getHeight(); ++y) {
		for (unsigned int x = 0; x < map->getWidth(); ++x) {
			unsigned int currentRadSq = 0;
			if (x < centreX) {
				currentRadSq += ((centreX - x) * (centreX - x));
			} else {
				currentRadSq += ((x - centreX) * (x - centreX));
			}
			if (y < centreY) {
				currentRadSq += ((centreY - y) * (centreY - y));
			} else {
				currentRadSq += ((y - centreY) * (y - centreY));
			}
			if (currentRadSq < radSq) {
				adjustTile(x, y, (signed int)sqrt(radSq - currentRadSq));
			}
		}
	}
}

void digHole(const unsigned int centreX, const unsigned int centreY,
             const unsigned int radius, const signed int height) {
	unsigned int radSq = radius * radius;
	for (unsigned int y = 0; y < map->getHeight(); ++y) {
		for (unsigned int x = 0; x < map->getWidth(); ++x) {
			unsigned int currentRadSq = (((x - centreX) * (x - centreX)) +
			                             ((y - centreY) * (y - centreY)));
			if (currentRadSq < radSq) {
				adjustTile(x, y, 0 - (signed int)sqrt(radSq - currentRadSq));
			}
		}
	}
}

